import 'package:flutter/material.dart';

///
/**
 * With Flutter, we have two options:
 * Supply an onChanged callback to a TextField
 * Use a TextEditingController
 */
///

class HandlingChangesToTextFieldDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Handling changes to a text field Demo'),
      ),
      body: ListView(
        children: <Widget>[
          // 1. onChanged
          TextField(
            onChanged: (text) {
              print("First text field: $text");
            },
          ),
          // 2. Use a TextEditingController
          MyCustemForm(),
        ],
      ),
    );
  }
}

class MyCustemForm extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyCustemFormState();
  }
}

class _MyCustemFormState extends State<MyCustemForm> {
  final myController = TextEditingController();

  @override
  void initState() {
    super.initState();

    myController.addListener(_printLatestValue);
  }

  _printLatestValue() {
    print("Second text field: ${myController.text}");
  }

  @override
  void dispose() {
    myController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        TextField(
          controller: myController,
          decoration: InputDecoration(
            hintText: 'ss',
          ),
        ),
      ],
    );
  }
}
